﻿using ProjScan.Modules;
using ProjScan.Tools;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace ProjScan.Submodules.Custom_Rules_Check
{
    internal class StackTraceLogger : Singleton<StackTraceLogger>
    {
        private static List<KeyValuePair<string, string>> logEntries = new List<KeyValuePair<string, string>>();

        public void RecordStackTrace(string message)
        {
            string key = null;
            List<Type> list = new List<Type>();
            StackTrace stackTrace = new StackTrace(fNeedFileInfo: true);
            StackFrame[] frames = stackTrace.GetFrames();
            foreach (StackFrame stackFrame in frames)
            {
                list.Add(stackFrame.GetMethod().ReflectedType);
            }

            HashSet<Type> hashSet = new HashSet<Type>(Singleton<CustomRulesCheckModule>.Instance.GetRuleTypes());
            foreach (Type item2 in list)
            {
                if (hashSet.Contains(item2))
                {
                    key = item2.Name;
                }
            }

            KeyValuePair<string, string> item = new KeyValuePair<string, string>(key, message);
            logEntries.Add(item);
        }

        public void OutputAndClearLog()
        {
            foreach (KeyValuePair<string, string> logEntry in logEntries)
            {
                UnityEngine.Debug.LogWarning(logEntry);
            }

            logEntries.Clear();
        }
    }
}